package com.czapl.mygdxgame;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.czapl.api.IFieldSelectionListener;
import com.czapl.api.ITower;

public class ConstructableField extends Actor{
	private CellCoordinates m_cellCoordinates;
	private ITower m_tower = null;
	private ShapeRenderer m_shapeRenderer;
	private Rectangle m_area;
	private Color m_frameColor = new Color(1, 1, 1, 0.2f);
	private Color m_fillColor = new Color(1, 1, 1, 0.5f);
	private boolean m_isHighlighted = false;
	private IFieldSelectionListener m_listener;
	
	
	public ConstructableField( CellCoordinates cellCoordinates) {
		m_shapeRenderer = new ShapeRenderer();
		m_area = new Rectangle(cellCoordinates.getX()*GameBoard.TILE_SIZE, cellCoordinates.getY()*GameBoard.TILE_SIZE, 
				GameBoard.TILE_SIZE, GameBoard.TILE_SIZE);
		m_cellCoordinates = cellCoordinates;
		setSize(GameBoard.TILE_SIZE, GameBoard.TILE_SIZE);
		addListener( new ClickListener() {
            @Override
            public void clicked(InputEvent event,float x,float y ) {
            	m_listener.onFieldSelected(ConstructableField.this);
            }
        } );
	}
	
	public CellCoordinates getCoordinates() {
		return m_cellCoordinates;
	}
	
	public boolean addTower(ITower tower) {
		if(m_tower != null) {
			return false;
		}
		m_tower = tower;
		return true;
	}
	
	public ITower getTower() {
		return m_tower;
	}
	
	public void draw (SpriteBatch batch, float parentAlpha) {
		if(m_isHighlighted) {
			batch.end();
			Gdx.gl.glEnable(GL20.GL_BLEND);
		    Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
			m_shapeRenderer.setProjectionMatrix(getStage().getCamera().combined);
			m_shapeRenderer.setColor(m_frameColor);
			m_shapeRenderer.begin(ShapeType.Filled);
			if(m_tower != null) {
				m_shapeRenderer.circle(m_area.x+m_area.width/2, m_area.y+m_area.height/2, m_tower.getRange());
			}
			else {
				m_shapeRenderer.rect(m_area.x, m_area.y, m_area.width, m_area.height);
			}
			m_shapeRenderer.end();
			m_shapeRenderer.setColor(m_fillColor);
			m_shapeRenderer.begin(ShapeType.Line);
			if(m_tower != null) {
				m_shapeRenderer.circle(m_area.x+m_area.width/2, m_area.y+m_area.height/2, m_tower.getRange());
			}
			else {
				m_shapeRenderer.rect(m_area.x, m_area.y, m_area.width, m_area.height);
			}
			m_shapeRenderer.end();
			Gdx.gl.glDisable(GL20.GL_BLEND);
			batch.begin();
		}
	}

	public void setHighlighted(boolean highlighted) {
		m_isHighlighted = highlighted;
	}
	
	public boolean isHighlighted() {
		return m_isHighlighted;
	}
	
	public boolean toggleSelection() {
		m_isHighlighted = !m_isHighlighted;
		return m_isHighlighted;
	}

	public void setFieldSelectionListener(IFieldSelectionListener listener) {
		m_listener = listener;
	}

}
